// 🦜 : [AI generated] : Jianer to test the chain <-> Json. 
using System.Security.Cryptography;
using System.Text;
#if MY_TEST
using Xunit;


public class T03 {

    [Fact]
    public async Task TestPureTextPayload() {
        var td = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
        Directory.CreateDirectory(td);
        try {
            var k = "test";
            var v = "Hello World";
            var r = await ChainItem.DumpPayloadAndGetUrl(k, v, td);
            Assert.True(File.Exists(r));
            Assert.EndsWith(".txt", r);
            var c = await File.ReadAllTextAsync(r);
            Assert.Equal(v, c);
        } finally {
            Directory.Delete(td, true);
        }
    }

    [Fact]
    public async Task TestBinaryDocx() {
        var td = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
        Directory.CreateDirectory(td);
        try {
            var k = "sowDocx64";
            var b = new byte[] {0x50, 0x4B, 0x03, 0x04};
            var v = Convert.ToBase64String(b);
            var r = await ChainItem.DumpPayloadAndGetUrl(k, v, td);
            Assert.True(File.Exists(r));
            Assert.EndsWith(".docx", r);
            var rb = await File.ReadAllBytesAsync(r);
            Assert.Equal(b, rb);
        } finally {
            Directory.Delete(td, true);
        }
    }

    [Fact]
    public async Task TestPythonScript() {
        var td = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
        Directory.CreateDirectory(td);
        try {
            var k = "pyscript";
            var v = "print('test')";
            var r = await ChainItem.DumpPayloadAndGetUrl(k, v, td);
            Assert.True(File.Exists(r));
            Assert.EndsWith(".py", r);
            var c = await File.ReadAllTextAsync(r);
            Assert.Equal(v, c);
        } finally {
            Directory.Delete(td, true);
        }
    }

    [Fact]
    public async Task TestSameContentSameFile() {
        var td = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
        Directory.CreateDirectory(td);
        try {
            var k = "test";
            var v = "Same content";
            var r1 = await ChainItem.DumpPayloadAndGetUrl(k, v, td);
            var r2 = await ChainItem.DumpPayloadAndGetUrl(k, v, td);
            Assert.Equal(r1, r2);
            var fc = Directory.GetFiles(td).Length;
            Assert.Equal(1, fc);
        } finally {
            Directory.Delete(td, true);
        }
    }

    [Fact]
    public async Task TestDifferentKeySameContent() {
        var td = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
        Directory.CreateDirectory(td);
        try {
            var v = "Same content";
            var r1 = await ChainItem.DumpPayloadAndGetUrl("key1", v, td);
            var r2 = await ChainItem.DumpPayloadAndGetUrl("pyscript", v, td);
            Assert.NotEqual(r1, r2);
            Assert.EndsWith(".txt", r1);
            Assert.EndsWith(".py", r2);
            var fn = Path.GetFileNameWithoutExtension(r1);
            var fn2 = Path.GetFileNameWithoutExtension(r2);
            Assert.Equal(fn, fn2);
        } finally {
            Directory.Delete(td, true);
        }
    }
}


    #endif
